<?php
/**待受理
 * Created by leiying
 * Date: 2018-06-11
 * Time: 18:25:09
 */

namespace Admin\Controller;


class ApplyController extends AdminController
{
    private static $yunpianClient;

    /**修改项目名称
     * @author leiying
     */
    public function changeApplyName(){
        if(!in_array(DEPART,array(0,C("APPROVER")))){
            $this->error("仅限审批人操作");
        }
        if(IS_POST){
            $id = I('post.id',0);
            if(empty($id)){$this->error("请选择要操作的数据");}
            $apply_name = I('post.apply_name',"","trim");
            $res = M("apply")->where(array("id" => $id))->save(array("apply_name"=>$apply_name));
            if(is_numeric($res)){
                $this->success("修改成功");
            }else{
                $this->error("修改失败");
            }
        }else{
            $this->error('非法操作');
        }
    }

   
    /**【待受理】列表（审批人可看）
     * @author leiying
     */
    public function Apply1List(){
        $limit = LIMIT;
        $get = I('post.');

        $search_data = array();//筛选数据
        $where = array();//条件

        if(IS_POST){
            session('searchdata_Apply1List',$get);
        }else{
            if(empty(I('get.p'))){
                session('searchdata_Apply1List',null);
            }
            $get = $search_data = session('searchdata_Apply1List');
        }
        $where['a.status'] = 1;
        //搜索条件：编号
        if(isset($get['number']) && $get['number'] != ''){
            $search_data['number'] = trim($get['number']);
            $where['a.number'] = array('like','%'.$search_data['number'].'%');
        }
        //搜索条件：项目分类
        if(isset($get['p_id']) && $get['p_id'] != ''){
            $search_data['p_id'] = trim($get['p_id']);
            $where['a.p_id'] = array('like','%'.$search_data['p_id'].'%');
        }
        //搜索条件：申请人姓名
        if(isset($get['submitter']) && $get['submitter'] != ''){
            $search_data['submitter'] = trim($get['submitter']);
            $where['a.submitter'] = array('like','%'.$search_data['submitter'].'%');
        }
        //搜索条件：验证手机
        if(isset($get['validate_phone']) && $get['validate_phone'] != ''){
            $search_data['validate_phone'] = trim($get['validate_phone']);
            $where['a.validate_phone'] = array('like','%'.$search_data['validate_phone'].'%');
        }
        $count = M('apply')->alias("a")->where($where)->count();

        $Page = new  \Think\Page($count, $limit);// 实例化分页类 传入总记录数
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出

        //处理排序逻辑(默认主键倒序)
        $sort = array(
            'field'=>I('get.sort_field','id'),
            'type'=>I('get.sort_type','desc'),
        );
        $sortString = 'a.'.$sort['field'] . ' ' .$sort['type'];
        $this->assign('sort',$sort);

        $lists = M('apply')
            ->alias("a")
            ->field("a.*,b.name project_name")
            ->join(C('DB_PREFIX')."project b ON a.p_id = b.id","left")
            ->where($where)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order($sortString)
            ->select();
        foreach($lists as &$val){
            if(strtotime($val['wait_accept_time'].' 23:59:59') < time()){
                $val["wait_accept_time"] = '已超出';
            }else{
                $val["wait_accept_time"] = floor((strtotime($val['wait_accept_time'].' 23:59:59')-time())/86400).'天';
            }
        }

        $this->assign('lists',$lists);
        $this->assign('searchs',$search_data);

        $projects = M('project')->order("add_time desc")->select();
        $this->assign("projects",$projects);

        $p = I('get.p',"1");
        $this->assign("p",$p);
        $this->display();
    }

    /**【部门审批】（审批人，部门人员可看）(超级管理员和审批人可以查看,对应部门的人员可以查看和操作)
     * @author leiying
     */
    public function Apply2List(){
        $limit = LIMIT;
        $get = I('post.');
        $just_list = 2;
        if(C('USER_ADMINISTRATOR') == UID || in_array(DEPART,SPECIAL_DEPART_LIST) || DEPART == 0 ){
            $just_list = 1;
        }

        $search_data = array();//筛选数据
        $where = array();//条件

        if(IS_POST){
            session('searchdata_Apply2List',$get);
        }else{
            if(empty(I('get.p'))){
                session('searchdata_Apply2List',null);
            }
            $get = $search_data = session('searchdata_Apply2List');
        }
        $where['a.status'] = 2;
        //搜索条件：编号
        if(isset($get['number']) && $get['number'] != ''){
            $search_data['number'] = trim($get['number']);
            $where['a.number'] = array('like','%'.$search_data['number'].'%');
        }
        //搜索条件：项目分类
        if(isset($get['p_id']) && $get['p_id'] != ''){
            $search_data['p_id'] = trim($get['p_id']);
            $where['a.p_id'] = array('like','%'.$search_data['p_id'].'%');
        }
        //搜索条件：申请人姓名
        if(isset($get['submitter']) && $get['submitter'] != ''){
            $search_data['submitter'] = trim($get['submitter']);
            $where['a.submitter'] = array('like','%'.$search_data['submitter'].'%');
        }
        //搜索条件：验证手机
        if(isset($get['validate_phone']) && $get['validate_phone'] != ''){
            $search_data['validate_phone'] = trim($get['validate_phone']);
            $where['a.validate_phone'] = array('like','%'.$search_data['validate_phone'].'%');
        }

        if($just_list == 1){
            $count = M('apply')->alias("a")->where($where)->count();
        }else{
            $where["b.depart_id"] = DEPART;
            $where["b.status"] = 2;
            $count = M("depart_opinion")->alias('b')
                ->join(C('DB_PREFIX')."apply a ON b.apply_id = a.id","left")
                ->where($where)->count();
        }


        $Page = new  \Think\Page($count, $limit);// 实例化分页类 传入总记录数
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出

        //处理排序逻辑(默认主键倒序)
        $sort = array(
            'field'=>I('get.sort_field','id'),
            'type'=>I('get.sort_type','desc'),
        );
        $sortString = 'a.'.$sort['field'] . ' ' .$sort['type'];
        $this->assign('sort',$sort);
        if($just_list == 1){
            $lists = M('apply')
                ->alias("a")
                ->field("a.*,b.name project_name,c.nickname")
                ->join(C('DB_PREFIX')."project b ON a.p_id = b.id","left")
                ->join(C('DB_PREFIX')."member c ON a.operator_1 = c.uid","left")
                ->where($where)
                ->limit($Page->firstRow.','.$Page->listRows)
                ->order($sortString)
                ->select();
        }else{
            $where["b.depart_id"] = DEPART;
            $where["b.status"] = 2;
            $lists = M("depart_opinion")->alias('b')
                ->field("a.*,c.name project_name,d.nickname")
                ->join(C('DB_PREFIX')."apply a ON b.apply_id = a.id","left")
                ->join(C('DB_PREFIX')."project c ON a.p_id = c.id","left")
                ->join(C('DB_PREFIX')."member d ON a.operator_1 = d.uid","left")
                ->where($where)
                ->limit($Page->firstRow.','.$Page->listRows)
                ->order($sortString)
                ->select();
        }


        foreach($lists as &$val){
            if(strtotime($val['wait_complete_time'].' 23:59:59') < time()){
                $val["wait_complete_time"] = '已超出';
            }else{
                $val["wait_complete_time"] = floor((strtotime($val['wait_complete_time'].' 23:59:59')-time())/86400).'天';
            }
        }

        $this->assign('lists',$lists);
        $this->assign('just_list',$just_list);
        $this->assign('searchs',$search_data);

        $projects = M('project')->order("add_time desc")->select();
        $this->assign("projects",$projects);

        $p = I('get.p',"1");
        $this->assign("p",$p);
        $this->display();
    }

    /**【领导批注】(审批人，领导可看)
     * @author leiying
     */
    public function Apply3List(){
        $limit = LIMIT;
        $get = I('post.');
        $just_list = 2;
        if(C('USER_ADMINISTRATOR') == UID || DEPART == APPROVER || DEPART == 0){
            $just_list = 1;
        }else{
            if(DEPART != C("LEADER")){
                $this->display();exit;
            }
        }


        $search_data = array();//筛选数据
        $where = array();//条件

        if(IS_POST){
            session('searchdata_Apply3List',$get);
        }else{
            if(empty(I('get.p'))){
                session('searchdata_Apply3List',null);
            }
            $get = $search_data = session('searchdata_Apply3List');
        }
        $where['a.status'] = 3;
        //搜索条件：编号
        if(isset($get['number']) && $get['number'] != ''){
            $search_data['number'] = trim($get['number']);
            $where['a.number'] = array('like','%'.$search_data['number'].'%');
        }
        //搜索条件：项目分类
        if(isset($get['p_id']) && $get['p_id'] != ''){
            $search_data['p_id'] = trim($get['p_id']);
            $where['a.p_id'] = array('like','%'.$search_data['p_id'].'%');
        }
        //搜索条件：申请人姓名
        if(isset($get['submitter']) && $get['submitter'] != ''){
            $search_data['submitter'] = trim($get['submitter']);
            $where['a.submitter'] = array('like','%'.$search_data['submitter'].'%');
        }
        //搜索条件：验证手机
        if(isset($get['validate_phone']) && $get['validate_phone'] != ''){
            $search_data['validate_phone'] = trim($get['validate_phone']);
            $where['a.validate_phone'] = array('like','%'.$search_data['validate_phone'].'%');
        }

        $count = M('apply')->alias("a")->where($where)->count();


        $Page = new  \Think\Page($count, $limit);// 实例化分页类 传入总记录数
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出

        //处理排序逻辑(默认主键倒序)
        $sort = array(
            'field'=>I('get.sort_field','id'),
            'type'=>I('get.sort_type','desc'),
        );
        $sortString = 'a.'.$sort['field'] . ' ' .$sort['type'];
        $this->assign('sort',$sort);
        $lists = M('apply')
            ->alias("a")
            ->field("a.*,b.name project_name,c.nickname")
            ->join(C('DB_PREFIX')."project b ON a.p_id = b.id","left")
            ->join(C('DB_PREFIX')."member c ON a.operator_1 = c.uid","left")
            ->where($where)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order($sortString)
            ->select();
        foreach($lists as &$val){
            if(strtotime($val['wait_complete_time'].' 23:59:59') < time()){
                $val["wait_complete_time"] = '已超出';
            }else{
                $val["wait_complete_time"] = floor((strtotime($val['wait_complete_time'].' 23:59:59')-time())/86400).'天';
            }
        }

        $this->assign('lists',$lists);
        $this->assign('just_list',$just_list);
        $this->assign('searchs',$search_data);

        $projects = M('project')->order("add_time desc")->select();
        $this->assign("projects",$projects);

        $p = I('get.p',"1");
        $this->assign("p",$p);
        $this->display();
    }

    /**【批注汇总】(审批人)
     * @author leiying
     */
    public function Apply4List(){
        $limit = LIMIT;
        $get = I('post.');

        $search_data = array();//筛选数据
        $where = array();//条件

        if(IS_POST){
            session('searchdata_Apply4List',$get);
        }else{
            if(empty(I('get.p'))){
                session('searchdata_Apply4List',null);
            }
            $get = $search_data = session('searchdata_Apply4List');
        }
        $where['a.status'] = 4;
        //搜索条件：编号
        if(isset($get['number']) && $get['number'] != ''){
            $search_data['number'] = trim($get['number']);
            $where['a.number'] = array('like','%'.$search_data['number'].'%');
        }
        //搜索条件：项目分类
        if(isset($get['p_id']) && $get['p_id'] != ''){
            $search_data['p_id'] = trim($get['p_id']);
            $where['a.p_id'] = array('like','%'.$search_data['p_id'].'%');
        }
        //搜索条件：申请人姓名
        if(isset($get['submitter']) && $get['submitter'] != ''){
            $search_data['submitter'] = trim($get['submitter']);
            $where['a.submitter'] = array('like','%'.$search_data['submitter'].'%');
        }
        //搜索条件：验证手机
        if(isset($get['validate_phone']) && $get['validate_phone'] != ''){
            $search_data['validate_phone'] = trim($get['validate_phone']);
            $where['a.validate_phone'] = array('like','%'.$search_data['validate_phone'].'%');
        }
        $count = M('apply')->alias("a")->where($where)->count();

        $Page = new  \Think\Page($count, $limit);// 实例化分页类 传入总记录数
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出

        //处理排序逻辑(默认主键倒序)
        $sort = array(
            'field'=>I('get.sort_field','id'),
            'type'=>I('get.sort_type','desc'),
        );
        $sortString = 'a.'.$sort['field'] . ' ' .$sort['type'];
        $this->assign('sort',$sort);

        $lists = M('apply')
            ->alias("a")
            ->field("a.*,b.name project_name")
            ->join(C('DB_PREFIX')."project b ON a.p_id = b.id","left")
            ->where($where)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order($sortString)
            ->select();
        foreach($lists as &$val){
            if(strtotime($val['wait_accept_time'].' 23:59:59') < time()){
                $val["wait_accept_time"] = '已超出';
            }else{
                $val["wait_accept_time"] = floor((strtotime($val['wait_accept_time'].' 23:59:59')-time())/86400).'天';
            }
        }

        $this->assign('lists',$lists);
        $this->assign('searchs',$search_data);

        $projects = M('project')->order("add_time desc")->select();
        $this->assign("projects",$projects);

        $p = I('get.p',"1");
        $this->assign("p",$p);
        $this->display();
    }

    /**查看部门审批进度
     * @author leiying
     */
    public function departProgress(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $this->error("非法操作");
        }else{
            $id = I('get.id');
            if(empty($id)){
                $this->error("请选择需要操作的数据");
            }
            $lists = M("depart_opinion")->alias("a")
                ->field("a.*,b.name depart_name")->join(C('DB_PREFIX')."department b ON a.depart_id = b.id")
                ->where(array("a.apply_id"=>$id))->select();
            $this->assign('lists',$lists);
            $p = I('get.p',"1");
            $this->assign("p",$p);
            $this->display();
        }

    }

    /**审批完成
     * @author leiying
     */
    public function Apply5List(){
        $limit = LIMIT;
        $get = I('post.');

        $search_data = array();//筛选数据
        $where = array();//条件

        if(IS_POST){
            session('searchdata_Apply5List',$get);
        }else{
            if(empty(I('get.p'))){
                session('searchdata_Apply5List',null);
            }
            $get = $search_data = session('searchdata_Apply5List');
        }
        $where['a.status'] = 5;
        //搜索条件：编号
        if(isset($get['number']) && $get['number'] != ''){
            $search_data['number'] = trim($get['number']);
            $where['a.number'] = array('like','%'.$search_data['number'].'%');
        }
        //搜索条件：项目分类
        if(isset($get['p_id']) && $get['p_id'] != ''){
            $search_data['p_id'] = trim($get['p_id']);
            $where['a.p_id'] = array('like','%'.$search_data['p_id'].'%');
        }
        //搜索条件：申请人姓名
        if(isset($get['submitter']) && $get['submitter'] != ''){
            $search_data['submitter'] = trim($get['submitter']);
            $where['a.submitter'] = array('like','%'.$search_data['submitter'].'%');
        }
        //搜索条件：验证手机
        if(isset($get['validate_phone']) && $get['validate_phone'] != ''){
            $search_data['validate_phone'] = trim($get['validate_phone']);
            $where['a.validate_phone'] = array('like','%'.$search_data['validate_phone'].'%');
        }
        $count = M('apply')->alias("a")->where($where)->count();

        $Page = new  \Think\Page($count, $limit);// 实例化分页类 传入总记录数
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出

        //处理排序逻辑(默认主键倒序)
        $sort = array(
            'field'=>I('get.sort_field','id'),
            'type'=>I('get.sort_type','desc'),
        );
        $sortString = 'a.'.$sort['field'] . ' ' .$sort['type'];
        $this->assign('sort',$sort);

        $lists = M('apply')
            ->alias("a")
            ->field("a.*,b.name project_name,c.nickname")
            ->join(C('DB_PREFIX')."project b ON a.p_id = b.id","left")
            ->join(C('DB_PREFIX')."member c ON a.operator_3 = c.uid","left")
            ->where($where)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order($sortString)
            ->select();
        $this->assign('lists',$lists);
        $this->assign('searchs',$search_data);
        $projects = M('project')->order("add_time desc")->select();
        $this->assign("projects",$projects);
        $p = I('get.p',"1");
        $this->assign("p",$p);
        $this->display();
    }

    /**【受理被拒】列表（审批人可看）
     * @author leiying
     */
    public function Apply6List(){
        $limit = LIMIT;
        $get = I('post.');

        $search_data = array();//筛选数据
        $where = array();//条件

        if(IS_POST){
            session('searchdata_Apply6List',$get);
        }else{
            if(empty(I('get.p'))){
                session('searchdata_Apply6List',null);
            }
            $get = $search_data = session('searchdata_Apply6List');
        }
        $where['a.status'] = 6;
        //搜索条件：编号
        if(isset($get['number']) && $get['number'] != ''){
            $search_data['number'] = trim($get['number']);
            $where['a.number'] = array('like','%'.$search_data['number'].'%');
        }
        //搜索条件：项目分类
        if(isset($get['p_id']) && $get['p_id'] != ''){
            $search_data['p_id'] = trim($get['p_id']);
            $where['a.p_id'] = array('like','%'.$search_data['p_id'].'%');
        }
        //搜索条件：申请人姓名
        if(isset($get['submitter']) && $get['submitter'] != ''){
            $search_data['submitter'] = trim($get['submitter']);
            $where['a.submitter'] = array('like','%'.$search_data['submitter'].'%');
        }
        //搜索条件：验证手机
        if(isset($get['validate_phone']) && $get['validate_phone'] != ''){
            $search_data['validate_phone'] = trim($get['validate_phone']);
            $where['a.validate_phone'] = array('like','%'.$search_data['validate_phone'].'%');
        }
        $count = M('apply')->alias("a")->where($where)->count();

        $Page = new  \Think\Page($count, $limit);// 实例化分页类 传入总记录数
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出

        //处理排序逻辑(默认主键倒序)
        $sort = array(
            'field'=>I('get.sort_field','id'),
            'type'=>I('get.sort_type','desc'),
        );
        $sortString = 'a.'.$sort['field'] . ' ' .$sort['type'];
        $this->assign('sort',$sort);

        $lists = M('apply')
            ->alias("a")
            ->field("a.*,b.name project_name,c.nickname")
            ->join(C('DB_PREFIX')."project b ON a.p_id = b.id","left")
            ->join(C('DB_PREFIX')."member c ON a.operator_1 = c.uid","left")
            ->where($where)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order($sortString)
            ->select();

        $this->assign('lists',$lists);
        $this->assign('searchs',$search_data);
        $projects = M('project')->order("add_time desc")->select();
        $this->assign("projects",$projects);
        $p = I('get.p',"1");
        $this->assign("p",$p);
        $this->display();
    }


    /**批量操作
     * @param null $method
     * @author leiying
     */
    public function changeStatus($method=null){
        $id = I('post.id');
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        switch ( strtolower($method) ){
            case 'deletegroup':
                $res = M("apply")->where(array("id"=>array('in',$id)))->delete();
                if($res){
                    $this->success("删除成功");
                }else{
                    $this->error("删除失败");
                }
                break;
            default:
                $this->error($method.'参数非法');
        }
    }



    /**编辑
     * @author leiying
     */
    public function ApplyEdit(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $post = I('post.');

            $id = $post['id'];
            $p_id = $post['p_id'];
            if(empty($id) || empty($p_id)){
                $this->error('请选择需要操作的数据');
            }
            /*处理参数(请根据业务逻辑适当修改)*/
            $operate = isset($post['operate'])?(int)$post['operate']:0;
            switch($operate){
                case 1:
                    $check_depart = isset($post["check_depart"])?$post["check_depart"]:array();
                    if(empty($check_depart) || !is_array($post["check_depart"])){
                        $this->error("请选择部门");
                    }

                    /*获取项目等待完成天数*/
                    $return = M("project")->where(array("id"=>$p_id))->getField("complete_day");
                    $complete_day = $return?$return:10;

                    M()->startTrans();
                    $save_data = array(
                        "operator_1" => UID,
                        "accept_time" => time(),
                        'wait_complete_time' => date("Y-m-d",strtotime("+$complete_day day")),
                        "status" => 2,
                    );

                    $res = M('apply')->where(array("id"=>$id,"status"=>1))->save($save_data);
                    if($res){

                        $insert_data = array();
                        foreach($check_depart as $key=>$row){
                            $insert_data[$key] = array(
                                "apply_id" => $id,
                                "depart_id" => $row,
                                "opinion" => '',
                                "write_time" => 0,
                                "uid" => 0,
                                'status' => 2,
                                "add_time" => time(),
                            );
                        }
                        $res = M("depart_opinion")->addAll($insert_data);
                        if($res){
                            /*给部门人员发送短信*/
                            $mobile_list = M("member")->alias("a")
                                ->join(C('DB_PREFIX')."ucenter_member b ON a.uid = b.id","left")
                                ->where(array("a.d_id"=>array('in',$check_depart)))
                                ->getField("mobile",true);

                            if($mobile_list){
                                $mobile = implode(',',$mobile_list);
                                $project_name = M("project")->where(array("id"=>$p_id))->getField("name");
                                vendor('ypsms','','.class.php');
                                $ypsms = new \ypsms();
                                $res_data = $ypsms->multi_send('2373022',$mobile,$project_name);
                            }

                            M()->commit();
                            $this->success('操作成功');
                        }else{
                            M()->rollback();
                            $this->error('操作失败');
                        }
                    }else{
                        M()->rollback();
                        $this->error('操作失败');
                    }
                    break;
                case 2:
                    //拒绝理由
                    $refuse_reason = trim($post['refuse_reason']);
                    if(empty($refuse_reason)){
                        $this->error("拒绝理由不能为空");
                    }
                    $data = array(
                        'operator_1' => UID,
                        'refuse_reason' => $refuse_reason,
                        'accept_time' => time(),
                        'status' => 6,
                    );
                    $res = M('apply')->where(array("id"=>$id,"status"=>1))->save($data);
                    if($res){
                        $this->success('操作成功');
                    }else{
                        $this->error('操作失败');
                    }
                    break;
                default:
                    $this->error("非法操作，请联系开发人员");
                    break;
            }

        }else{
            $id = I('get.id');
            if(empty($id)){
                $this->error("请选择需要操作的数据");
            }
            $res = M('apply')->alias("a")->field("a.*,b.name project_name")->join(C('DB_PREFIX')."project b ON a.p_id = b.id","left")->where(array('a.id'=>$id))->find();
            $this->assign('res',$res);

            $departments = M("department")->where(array("status"=>array("neq",2)))->order("add_time desc")->select();
            $this->assign("departments",$departments);

            $p = I('get.p',"1");
            $this->assign('p',$p);
            $this->display();
        }
    }

    /**部门审批(对应部门的人员可以操作)
     * @author leiying
     */
    public function Apply2Edit(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $post = I('post.');
            $id = $post['id'];
            if(empty($id)){
                $this->error('请选择需要操作的数据');
            }
			if(in_array(DEPART,array(0,C("LEADER")))){
				$this->error('身份不合法');
			}
            /*处理参数(请根据业务逻辑适当修改)*/
            $apply_id = isset($post['id'])?(int)$post['id']:0;
            $p_id = isset($post['p_id'])?(int)$post['p_id']:0;
            $depart_id = DEPART;
            $opinion = isset($post['opinion'])?$post['opinion']:'';
            if(empty($opinion)){$this->error("部门意见不能为空");}
            $save_data = array(
                'opinion' => $opinion,
                'write_time' => time(),
                'uid' => UID,
                'status' => 1
            );
            M()->startTrans();
            $res = M("depart_opinion")->where(array("apply_id"=>$apply_id,"depart_id"=>$depart_id,"status"=>2))->save($save_data);
            if($res){
                //检查部门审批是否全部完成
                $res = M("depart_opinion")->where(array("apply_id"=>$apply_id,"status"=>2))->find();
                if($res === null){
                    $update_data = array(
                        'status' => 3,//领导批注
                    );
                    $res = M("apply")->where(array("id"=>$apply_id,"status"=>2))->save($update_data);
                    if(!$res){
                        M()->rollback();
                        $this->error("审批失败");
                    }
                    /*短信通知领导批注 角色-20（写死）*/
                    $mobile_list = M("auth_group_access")->alias("a")
                        ->join(C('DB_PREFIX')."ucenter_member b ON a.uid = b.id","left")
                        ->where(array("a.group_id"=>20))
                        ->getField("mobile",true);
                    if($mobile_list){
                        $mobile = implode(',',$mobile_list);
                        $project_name = M("project")->where(array("id"=>$p_id))->getField("name");
                        vendor('ypsms','','.class.php');
                        $ypsms = new \ypsms();
                        $res_data = $ypsms->multi_send('2373024',$mobile,$project_name);
                    }
                }
                M()->commit();
                $this->success("审批意见已提交");
            }else{
                M()->rollback();
                $this->error("异常错误，请联系开发人员");
            }
        }else{
            $id = I('get.id');
            if(empty($id)){
                $this->error("请选择需要操作的数据");
            }
            $res = M('apply')->alias("a")->field("a.*,b.name project_name")->join(C('DB_PREFIX')."project b ON a.p_id = b.id","left")->where(array('a.id'=>$id))->find();
            $this->assign('res',$res);


            $p = I('get.p',"1");
            $this->assign('p',$p);
            $this->display();
        }
    }

    /**领导批注
     * @author leiying
     */
    public function Apply3Edit(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $post = I('post.');
            $id = $post['id'];
            if(empty($id)){
                $this->error('请选择需要操作的数据');
            }
            /*处理参数(请根据业务逻辑适当修改)*/

            $leader_comment = isset($post['leader_comment'])?$post['leader_comment']:'';
            if(empty($leader_comment)){
                $this->error("批注不能为空");
            }
            $update_data = array(
                'operator_2' => UID,
                'leader_comment' => $leader_comment,
                'leader_time' => time(),
                'status' => 4,//审批汇总
            );
            $res = M("apply")->where(array("id"=>$id,"status"=>3))->save($update_data);
            if($res){
                $this->success("批注成功");
            }else{
                $this->error("批注失败");
            }
        }else{
            $id = I('get.id');
            if(empty($id)){
                $this->error("请选择需要操作的数据");
            }
            $res = M('apply')->alias("a")->field("a.*,b.name project_name")->join(C('DB_PREFIX')."project b ON a.p_id = b.id","left")->where(array('a.id'=>$id))->find();
            $this->assign('res',$res);

            $opinions = M("depart_opinion")->alias("a")
                ->field("a.*,b.nickname,c.name depart_name")
                ->join(C('DB_PREFIX')."member b ON a.uid = b.uid","left")
                ->join(C('DB_PREFIX')."department c ON a.depart_id = c.id","left")
                ->where(array("apply_id"=>$id))->select();
            $this->assign("opinions",$opinions);

            $p = I('get.p',"1");
            $this->assign('p',$p);
            $this->display();
        }
    }

    /**审批汇总
     * @author leiying
     */
    public function Apply4Edit(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $post = I('post.');
            $id = $post['id'];
            if(empty($id)){
                $this->error('请选择需要操作的数据');
            }
            /*处理参数(请根据业务逻辑适当修改)*/
            $collect_opinion = isset($post['editorValue'])?$post['editorValue']:'';
            if(empty($collect_opinion)){
                $this->error("审批汇总不能为空");
            }
            if(isset($post["deliver_tracking"])){
                $deliver_tracking = isset($post['deliver_tracking'])?$post['deliver_tracking']:'';
                if(empty($deliver_tracking)){
                    $this->error("请填写快递单号");
                }
            }else{
                $deliver_tracking = '';
            }

            $check_account = '';
            for($i = 1;$i <= 6 ;$i++){
                $check_account .= chr(rand(97,122));
            }

            $check_pwd = rand(100000,999999);
            $update_data = array(
                'operator_3' => UID,
                'deliver_tracking' => $deliver_tracking,
                'collect_opinion' => $collect_opinion,
                'complete_time' => time(),
                'check_account' => $check_account,
                'check_pwd' => $check_pwd,
                'status' => 5,//受理完成
            );
            $res = M("apply")->where(array("id"=>$id,"status"=>4))->save($update_data);
            if($res){
                $this->success("汇总成功");
            }else{
                $this->error("汇总失败");
            }
        }else{
            $id = I('get.id');
            if(empty($id)){
                $this->error("请选择需要操作的数据");
            }
            $res = M('apply')->alias("a")->field("a.*,b.name project_name")->join(C('DB_PREFIX')."project b ON a.p_id = b.id","left")->where(array('a.id'=>$id))->find();
            $this->assign('res',$res);

            $opinions = M("depart_opinion")->alias("a")
                ->field("a.*,b.nickname,c.name depart_name")
                ->join(C('DB_PREFIX')."member b ON a.uid = b.uid","left")
                ->join(C('DB_PREFIX')."department c ON a.depart_id = c.id","left")
                ->where(array("apply_id"=>$id))->select();
            $this->assign("opinions",$opinions);

            $p = I('get.p',"1");
            $this->assign('p',$p);
            $this->display();
        }
    }

    /**受理完成
     * @author leiying
     */
    public function Apply5Edit(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
           $this->error("非法操作");
        }else{
            $id = I('get.id');
            if(empty($id)){
                $this->error("请选择需要操作的数据");
            }
            $res = M('apply')->alias("a")->field("a.*,b.name project_name")->join(C('DB_PREFIX')."project b ON a.p_id = b.id","left")->where(array('a.id'=>$id))->find();
            $this->assign('res',$res);

            $opinions = M("depart_opinion")->alias("a")
                ->field("a.*,b.nickname,c.name depart_name")
                ->join(C('DB_PREFIX')."member b ON a.uid = b.uid","left")
                ->join(C('DB_PREFIX')."department c ON a.depart_id = c.id","left")
                ->where(array("apply_id"=>$id))->select();
            $this->assign("opinions",$opinions);

            $p = I('get.p',"1");
            $this->assign('p',$p);
            $this->display();
        }
    }

}